package com.dong.boot.controller.emp;

import com.dong.boot.model.*;
import com.dong.boot.service.emp.EmployeeService;
import com.dong.boot.utils.POIUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/employee/basic")
public class EmployeeBasicController {
    @Autowired
    EmployeeService employeeService;

    @GetMapping("/")
    public RespPageBean getAllEmployee(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "10") Integer pageSize,
                                       Employee employee, Date[] beginEndScope){
        return employeeService.getAllEmployee(pageNo,pageSize,employee,beginEndScope);
    }

    @PutMapping("/")
    public RespBean addEmployee(@RequestBody  Employee employee){
        if (employeeService.addEmployee(employee)){
            return RespBean.ok("新增成功");
        }
        return RespBean.error("新增失败");
    }

    @PostMapping("/")
    public RespBean updateEmployee(@RequestBody Employee employee){
        if (employeeService.updateEmployee(employee)){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }

    @GetMapping("/politicsstatus")
    public List<Politicsstatus> getAllPoliticsStatus(){
        return employeeService.getAllPoliticsStatus();
    }

    @GetMapping("/nations")
    public List<Nation> getAllnations(){
        return employeeService.getAllnations();
    }

    @GetMapping("/jobLevels")
    public List<JobLevel> getAllJobLevel(){
        return employeeService.getAllJobLevel();
    }

    @GetMapping("/positions")
    public List<Position> getAllPositions(){
        return employeeService.getAllPositions();
    }

    @GetMapping("/departments")
    public List<Department> getAllDepartments(){
        return employeeService.getAllDepartments();
    }

    private List<Department> getAllDepartmentWithOutChildren(){
        return  employeeService.getAllDepartmentWithOutChildren();
    }

    @DeleteMapping("/{id}")
    public RespBean deleteEmp(@PathVariable Integer id){
        if (employeeService.deleteEmp(id)){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @GetMapping("/export")
    public ResponseEntity<byte[]> employeeExport(){
        List<Employee> emps = (List<Employee>) employeeService.getAllEmployee(null, null, new Employee(),null).getData();
        return POIUtil.export(emps);
    }

    @PostMapping("/import")
    public RespBean employeeImport(MultipartFile file){
        //1.对上传的进行解析
       List<Employee> list =  POIUtil.excel2Employee(file,getAllPoliticsStatus(),getAllDepartmentWithOutChildren(),getAllJobLevel(),getAllnations(),getAllPositions());
       employeeService.batchAddEmployee(list);
       return RespBean.ok("导入成功");
    }
}
